package com.pancm.datacollector.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
import org.yaml.snakeyaml.Yaml;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * YAML配置管理器实现类
 * 负责从YAML配置文件中读取和管理配置项
 */
@Component
public class YamlConfigurationManager implements ConfigurationManager {
    
    @Autowired
    private Environment environment;
    
    private final Map<String, Object> configurationMap = new HashMap<>();
    
    @PostConstruct
    public void init() {
        // 在Spring容器初始化后自动加载配置
        loadConfiguration();
    }
    
    /**
     * 加载配置文件
     */
    private void loadConfiguration() {
        try {
            // 创建YAML解析器
            Yaml yaml = new Yaml();
            
            // 获取classpath下的application.yml文件
            Resource resource = new ClassPathResource("application.yml");
            InputStream inputStream = resource.getInputStream();
            
            // 添加类型转换
            @SuppressWarnings("unchecked")
            Map<String, Object> configurationMap = (Map<String, Object>) yaml.load(inputStream);
            
            // 使用临时变量赋值
            if (configurationMap != null) {
                configurationMap.clear();
                configurationMap.putAll(configurationMap);
            }
            
            inputStream.close();
        } catch (IOException | ClassCastException e) {
            // 添加 ClassCastException 异常处理
            configurationMap.clear();
        }
    }
    
    @Override
    public String getStringValue(String key) {
        return getStringValue(key, null);
    }
    
    @Override
    public String getStringValue(String key, String defaultValue) {
        return environment.getProperty(key, defaultValue);
    }
    
    @Override
    public Integer getIntegerValue(String key) {
        return getIntegerValue(key, null);
    }
    
    @Override
    public Integer getIntegerValue(String key, Integer defaultValue) {
        String value = environment.getProperty(key);
        if (value == null) {
            return defaultValue;
        }
        try {
            return Integer.parseInt(value);
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }
    
    @Override
    public Boolean getBooleanValue(String key) {
        String value = environment.getProperty(key);
        return value != null ? Boolean.parseBoolean(value) : null;
    }
    
    @Override
    @SuppressWarnings("unchecked")
    public <T> List<T> getListValue(String key) {
        Object value = environment.getProperty(key);
        if (value instanceof List) {
            return (List<T>) value;
        }
        return new ArrayList<>();
    }
    
    @Override
    @SuppressWarnings("unchecked")
    public <K, V> Map<K, V> getMapValue(String key) {
        Object value = environment.getProperty(key);
        if (value instanceof Map) {
            return (Map<K, V>) value;
        }
        return new HashMap<>();
    }
} 